package com.work;

import java.util.*;

public class Main {
    public static void main(String[] args) {
//        TreeSet<Teacher> treeSet = new TreeSet<>(new Comparator<Teacher>() {
//            @Override
//            public int compare(Teacher o1, Teacher o2) {
//                int result = o1.age - o2.age;
//                if (result == 0) {
//                    return o1.name.compareTo(o2.name);
//                }
//                return result;
//            }
//        });
//        Teacher teacher1 = new Teacher("lee", 18);
//        Teacher teacher2 = new Teacher("jack", 18);
//        treeSet.add(teacher1);
//        treeSet.add(teacher2);
//
//        for (Teacher teacher : treeSet) {
//            System.out.println(teacher);
//        }

//        List<Integer> list = new ArrayList<>();
//        list.add(10);
//        list.add(30);
//        list.add(20);


//        Collections.sort(list, new Comparator<Integer>() {
//            @Override
//            public int compare(Integer o1, Integer o2) {
////                return -o1.compareTo(o2);
//                return -(o1-o2);
//            }
//        });
//
//        for (Integer n : list) {
//            System.out.println(n);
//        }
//
//        // 将集合 list 里面的元素打乱
//        Collections.shuffle(list);
//        // ArrayList 可以使用普通 for 进行遍历
//        for (int i = 0; i < list.size(); i++) {
//            System.out.println(list.get(i));
//        }
//        int[] arr = generateArray(50000);
//
//        TimeTool.check("bubleSort", new Task() {
//            @Override
//            public void execute() {
//                int[] newArr = bubbleSort(Arrays.copyOf(arr, arr.length));
//            }
//        });
//
//
//        TimeTool.check("selectionSort", new Task() {
//            @Override
//            public void execute() {
//                int[] newArr = selectionSort(Arrays.copyOf(arr, arr.length));
//            }
//        });

        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int index = binarySearch(arr, 4);
        System.out.println(index);


    }

    public static int[] generateArray(int len) {
        Random random = new Random();
        int[] arr = new int[len];
        for (int i = 0; i < len; i++) {
            arr[i] = random.nextInt(10000);
        }
        return arr;
    }

    public static int[] bubbleSort(int[] arr) {
        int count = 0;  // 统计交换次数
        int cycle = 0; // 统计循环次数
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                cycle++;
                if (arr[j] > arr[j + 1]) {
                    count++;
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
        }
        System.out.println("selectionSort 循环次数" + cycle + ", 交换次数: " + count);
        return arr;
    }


    public static int binarySearch(int[] arr, int num) {
        // 1. 定义两个变量，表示要查找的范围，默认 min = 0, max = 最大索引
        int min = 0;
        int max = arr.length - 1;

        // 2. 循环查找
        while (min <= max) {
            // 3. 计算 mid 的值
            int mid = (min + max) >> 1;
            if (arr[mid] == num) {
                return mid;
            } else if (arr[mid] > num) {
                max = mid - 1;
            } else {
                min = mid + 1;
            }
        }
        return -1;
    }

    public static int[] selectionSort(int[] arr) {
        int count = 0;  // 统计交换次数
        int cycle = 0; // 统计循环次数
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i; // 每轮循环最小值索引默认值
            for (int j = i + 1; j < arr.length; j++) {
                cycle++;
                if (arr[minIndex] > arr[j]) {
                    count++;
                    // 记录最小值索引
                    minIndex = j;
                }
            }
            // 内层循环完之后得到最小值的索引，进行元素的交换
            if (minIndex != i) {
                int tmp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = tmp;
            }
        }
        System.out.println("selectionSort 循环次数" + cycle + ", 交换次数: " + count);
        return arr;
    }


    public static int sum(int... nums) {
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            result += nums[i];
        }

        return result;

    }

}

class ComparatorImpl implements Comparator<Teacher> {
    @Override
    public int compare(Teacher o1, Teacher o2) {
        int result = o1.age - o2.age;
        return result == 0 ? o1.name.compareTo(o2.name) : result;
    }
}